package com.example.schoolnewsapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.schoolnewsapp.JavaBean.news_info;
import com.example.schoolnewsapp.databinding.FragmentSingleBinding;
import java.util.List;

public class NewsSingleAdapter extends RecyclerView.Adapter<NewsSingleAdapter.ViewHolder> {
    private final List<news_info> newsList;
    private final View.OnClickListener listener;
    private View.OnLongClickListener LongClickListener;


    public NewsSingleAdapter(List<news_info> newsList, View.OnClickListener listener) {
        this.newsList = newsList;
        this.listener= listener;
    }

    @NonNull
    @Override
    public NewsSingleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FragmentSingleBinding binding = FragmentSingleBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
        return new ViewHolder(binding);
    }

    //数据显示到视图上
    @Override
    public void onBindViewHolder(@NonNull NewsSingleAdapter.ViewHolder holder, int position) {
        news_info import_news = newsList.get(position);
        holder.binding.singleTitle.setText(import_news.getTitle());
        holder.binding.singleTime.setText(import_news.getUpdatedAt());

    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        FragmentSingleBinding binding;
        public ViewHolder(@NonNull FragmentSingleBinding binding) {
            super(binding.getRoot());
            this.binding=binding;
            this.itemView.setTag(this);
            this.itemView.setOnClickListener(listener);
            this.itemView.setOnLongClickListener(LongClickListener);
        }
    }
}
